package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.PDFUtil;
import com.zzyl.common.utils.file.FileUtils;
import com.zzyl.nursing.dto.HealthAssessmentDto;
import com.zzyl.oss.AliyunOSSOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.service.IHealthAssessmentService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 健康评估Controller
 * @author JBY
 * @date 2025-08-16
 */
@Api(tags = "健康评估管理")
@RestController
@RequestMapping("/nursing/healthAssessment")
public class HealthAssessmentController extends BaseController {
    @Autowired
    private IHealthAssessmentService healthAssessmentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    /**
     * 文件上传
     *
     * @param: String IdCardNo
     * @param: MultipartFile file
     * @return: AjaxResult
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AjaxResult upload(@ApiParam("身份证号") String idCardNo, @ApiParam("体检报告") MultipartFile file) {
        try {
            // 1. 校验身份证是否为空
            if (StrUtil.isEmpty(idCardNo) || "null".equals(idCardNo)) {
                // 1.1 如果是空就不能上传, 返回错误
                return AjaxResult.error("请输入身份证号.");
            }
            // 2. 判断文件是否为空
            if (Objects.isNull(file)) {
                // 2.1 如果文件是空就不能上传, 返回错误
                return AjaxResult.error("请上传文件.");
            }
            // 3. 上传文件
            String originalFilename = file.getOriginalFilename();
            String url = aliyunOSSOperator.upload(file.getBytes(), originalFilename);
            // 4. PDF转为字符串, 把身份证号, 字符存入Redis
            PDFUtil pdfUtil = new PDFUtil();
            String content = PDFUtil.pdfToString(file.getInputStream());
            redisTemplate.opsForHash().put(Constants.CACHE_HEALTH_ASSESSMENT, idCardNo, content);
            // 5. 把前端需要的信息存入AjaxResult 返回
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            //url与fileName都是url
            ajax.put("fileName", url);
            //若依框架自动获取文件名的方法
            ajax.put("newFileName", FileUtils.getName(url));
            //原文件名
            ajax.put("originalFileName", originalFilename);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询健康评估列表
     */
    @ApiOperation("查询健康评估列表")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:list')")
    @GetMapping("/list")
    public TableDataInfo<List<HealthAssessment>> list(@ApiParam("健康评估查询条件") HealthAssessment healthAssessment) {
        startPage();
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        return getDataTable(list);
    }

    /**
     * 导出健康评估列表
     */
    @ApiOperation("导出健康评估列表")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:export')")
    @Log(title = "健康评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "健康评估查询条件") HttpServletResponse response, HealthAssessment healthAssessment) {
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        ExcelUtil<HealthAssessment> util = new ExcelUtil<HealthAssessment>(HealthAssessment.class);
        util.exportExcel(response, list, "健康评估数据");
    }

    /**
     * 获取健康评估详细信息
     */
    @ApiOperation("获取健康评估详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:query')")
    @GetMapping(value = "/{id}")
    public R<HealthAssessment> getInfo(@ApiParam("健康评估ID") @PathVariable("id") Long id) {
        return R.ok(healthAssessmentService.selectHealthAssessmentById(id));
    }

    /**
     * 智能评测
     */
    @ApiOperation("新增健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    @Log(title = "健康评估", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@ApiParam("健康评估信息") @RequestBody HealthAssessmentDto healthAssessmentDto) {
        return R.ok(healthAssessmentService.insertHealthAssessment(healthAssessmentDto));
    }

    /**
     * 修改健康评估
     */
    @ApiOperation("修改健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:edit')")
    @Log(title = "健康评估", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("健康评估信息") @RequestBody HealthAssessment healthAssessment) {
        return toAjax(healthAssessmentService.updateHealthAssessment(healthAssessment));
    }

    /**
     * 删除健康评估
     */
    @ApiOperation("删除健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:remove')")
    @Log(title = "健康评估", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("健康评估ID数组") @PathVariable Long[] ids) {
        return toAjax(healthAssessmentService.deleteHealthAssessmentByIds(ids));
    }
}
